Method and Device for Recording System Log

ABSTRACT

Provided are a method and device for recording a system log. The method includes: acquiring the sequence information of a virtual log file in a system, wherein the virtual log file is a preset file for the system, and the virtual log file includes the sequence information, and the sequence information is used for determining a position of a log file which is written last time by the system; and determining the log file which is written last time by the system according to the sequence information of the virtual log file, and writing the log in the determined log file. By the disclosure, the sequence of system log recording is not influenced by changes in the system time.

TECHNICAL FIELD

The disclosure relates to the data processing field, and in particularto a method and device for recording a system log.

BACKGROUND

In various daily systems, log recording is a means widely used forinformation recording and fault location. When log files are recorded, amode of cyclic covering of multiple log files is commonly used. Thereare multiple log files, and log recording, particularly a recordingsequence of logs, should not be influenced by the restart of a system ingeneral when the system is restarted. Therefore, how to determine acorrect log file for continuing recording becomes a problem after thesystem is restarted.

For the above-mentioned problem, solutions in the relate art are asfollows:

1) for a system without any requirement on a recording time length ofthe log files, a capacity of each log file is generally fixed. And aftersuch a system is restarted, the last log file where a log is recordedbefore restart can be judged according to the capacities of the logfiles; but however, the method is inapplicable to a system with arequirement on the recording time length of the log files.

2) for the system with the requirement on the recording time length ofthe log files, the capacity of each log file is unfixed, so the last logfile where the log is recorded before restart cannot be judged accordingto the capacities of the log files. At the moment, timestamps aregenerally added into names of the log files, so that the last log filewhere the log is recorded before restart can be determined forcontinuing recording according to the timestamps after the system isrestarted; but a shortcoming of the method is influenced by system time,and once the system time is changed to be earlier, the last recordingfile cannot be correctly determined after the system is restarted, whichfurther causes the recording sequence disorder of the log files.

For the problem of log file recording sequence disorder caused by theinfluence of changing of the system time on a log recording process ofthe system with the requirement on the recording time length of the logfiles in the related art, none effective solution is provided.

SUMMARY

The embodiments of the disclosure provide a method and device forrecording a system log, so as to at least solve the problem in therelated that log file recording sequence disorder caused by theinfluence of changing of system time on a log recording process of asystem with a requirement on a recording time length of log files.

In order to solve the above-mentioned problem, provides a method forrecording a system log is provided according to one aspect of thedisclosure, the method includes the following steps: acquiring sequenceinformation of a virtual log file in a system, wherein the virtual logfile is a preset file for the system, and the virtual log file includesthe sequence information, and the sequence information is used fordetermining a position of a log file which is written last time by thesystem; and determining the log file which is written last time by thesystem according to the sequence information of the virtual log file,and writing the determined log file.

In an example embodiment, before acquiring the sequence information ofthe virtual log file in the system, the method further includes:determining a maximum value N of the number of log files required to bereserved in the system according to a requirement of the system on alength of time when a log is written; creating N+1 sequence numbers,recorded as 1, 2 . . . N, N+1 respectively, the sequence numberscorresponding to the log files which are sequentially created, wherein asequence number of a log file which is created for the first time is 1,a sequence number of a log file which is created for the (N+1)th time isN+1, a sequence number of a log file which is created for the (N+2)thtime is 1, and so on; and generating the sequence information of thevirtual log file according to the created sequence numbers and a logrecording status of a current log file.

In an example embodiment, generating the sequence information of thevirtual log file according to the created sequence numbers and the logrecording status of the current log file includes: sequentially judgingwhether or not there is a log recorded under each of the createdsequence numbers; and acquiring a minimum sequence number n fromsequence numbers under which there are no logs recorded, and generatingthe sequence information of the virtual log file according to theminimum sequence number n, wherein the sequence information is used forindicating that the position of the log file which is written last timeby the system is a log file with a sequence number n−1 is located.

A device for recording a system log is provided according to anotheraspect of the disclosure, the device includes: an acquisition element,configured to acquire sequence information of a virtual log file in asystem, wherein the virtual log file is a preset file for the system,and the virtual log file includes the sequence information, and thesequence information is used for determining a position of a log filewhich is written last time by the system; and a first determinationelement, configured to determine the log file which is written last timeby the system according to the sequence information of the virtual logfile, and write the determined log file.

In an example embodiment, the device further includes: a seconddetermination element, configured to determine a maximum value N of thenumber of log files required to be reserved in the system according to arequirement of the system on a length of time when a log is written,before acquiring the sequence information of the virtual log file in thesystem; a creation element, configured to create N+1 sequence numbers,recorded as 1, 2 . . . N, N+1 respectively, the sequence numberscorresponding to log files which are sequentially created, wherein asequence number of a log file which is created for the first time is 1,a sequence number of a log file which is created for the (N+1)th time isN+1, a sequence number of a log file which is created for the (N+2)thtime is 1, and so on; and a generation element, configured to generatethe sequence information of the virtual log file according to thecreated sequence numbers and a log recording status of a current logfile.

In an example embodiment, the generation element includes: a judgmentcomponent, configured to sequentially judge whether or not there is alog recorded under each of the created sequence numbers; and ageneration component, configured to acquire a minimum sequence number nfrom sequence numbers under which there are no logs recorded, andgenerate the sequence information of the virtual log file according tothe minimum sequence number n, wherein the sequence information is usedfor indicating that log file which is written last time by the system isa log file with a sequence number n−1 is located.

The disclosure has beneficial effects as follows:

In the embodiments of the disclosure, when system log recording isperformed, the sequence information of the virtual log file is acquiredat first, the last log recording position in the system is determinedaccording to the sequence information of the virtual log file, and thesystem is controlled to sequentially continue performing log recordingafter the last log recording position in the system is determined; andby such a log recording manner, the problem in the related art that logfile recording sequence disorder caused by the influence of changing ofthe system time on the log recording process of the system with therequirement on the recording time length of the log files is effectivelysolved, and the sequence of system log recording is not influenced bychanges in the system time.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a flowchart of a method for recording a system log accordingto an example embodiment of the disclosure;

FIG. 2 is a structural diagram of a device for recording a system logaccording to an example embodiment of the disclosure;

FIG. 3 is a structural diagram of a device for recording a system logaccording to another example embodiment of the disclosure;

FIG. 4 is a structural diagram of a device for recording a system logaccording to another example embodiment of the disclosure; and

FIG. 5 is a processing schematic diagram of the reservation of logrecords in the last three days in the system according to an embodimentof the disclosure.

DETAILED DESCRIPTION OF EMBODIMENTS

In order to solve the problem in the related art that log file recordingsequence disorder caused by the influence of changing of system time ona log recording process of a system with a requirement on a recordingtime length of log files, a method and device for recording a system logis provided according to the disclosure. The disclosure is describedbelow with reference to the accompanying drawings and embodiments indetail. Note that, the embodiments of the disclosure and the features ofthe embodiments may be combined with each other if there is no conflict.

Embodiment 1

A method for recording a system log is provided according to an exampleembodiment of the disclosure. FIG. 1 is a flowchart of a method forrecording a system log according to an example embodiment of thedisclosure, and as shown in FIG. 1, the method includes the followingsteps:

S102: sequence information of a virtual log file in a system isacquired, wherein the virtual log file is a preset file for the system,and the virtual log file includes the sequence information, and thesequence information is used for determining a position of a log filewhich is written last time by the system; and

S104: the log file which is written last time by the system according tothe sequence information of the virtual log file is determined, and thedetermined log file is written.

In the example embodiment, when system log recording is performed, thesequence information of the virtual log file is acquired at first, thelast log recording position in the system is determined according to thesequence information of the virtual log file, and the system iscontrolled to sequentially continue performing log recording after thelast log recording position in the system is determined; and by such alog recording manner, the problem in the related art that log filerecording sequence disorder caused by the influence of changing ofsystem time on a log recording process of the system with a requirementon a recording time length of the log files is effectively solved, andthe sequence of system log recording is not influenced by changes in thesystem time.

In an example embodiment of the disclosure, the method is alsooptimized, and specifically, before the sequence information of thevirtual log file in the system is acquired, the method further includes:a maximum value N of the number of log files required to be reserved inthe system is determined according to the requirement of the system on alength of time when a log is written; N+1 sequence numbers are created,recorded as 1, 2 . . . N, N+1 respectively, the sequence numberscorresponding to log files which are sequentially created; and thesequence information of the virtual log file is generated according tothe created sequence numbers and a log recording status of a current logfile.

For example, if the system requires the reservation of log records ofthree days with one log file for one day, it can be determined that themaximum number N of the log files required to be reserved in the systemis 3, and after N is determined, the sequence numbers are created.Specifically, the number of the created sequence numbers is N+1=4, i.e.1, 2, 3 and 4 respectively. The four sequence numbers correspond to thelog files which are sequentially created, that is, a sequence numbercorresponding to a log file which is created by the system on the firstday is 1, a sequence number corresponding to a log file which is createdby the system on the second day is 2, a sequence number corresponding toa log file which is created by the system on the third day is 3; andbecause the system only requires the reservation of the log records ofthree days, a sequence number corresponding to a log file which iscreated by the system on the fourth day is 4, then the log file which iscreated on the first day and corresponds to the sequence number 1 iscleared, a sequence number corresponding to a log file which is createdby the system on the fifth day is 1, the log file which is created onthe second day and corresponds to the sequence number 2 is cleared, andso on. After the creation of the sequence numbers is finished, logrecording statuses (whether or not there are recorded logs) in the logfiles corresponding to the sequence number 1, the sequence number 2, thesequence number 3 and the sequence number 4 are sequentially judged, andthe sequence information of the virtual log file is generated accordingto a judgment result.

In addition, the disclosure also provides an example scheme forgenerating the sequence information of the virtual log file, andspecifically, the scheme includes the following steps: whether or notthere is a log recorded under each of the created sequence numbers issequentially judged; and a minimum sequence number n is acquired fromthe sequence numbers under which there are no logs recorded, and thesequence information of the virtual log file is generated according tothe minimum sequence number n, wherein the sequence information is usedfor indicating that the log file which is written last time by thesystem is a log file with a sequence number n−1 is located.

Embodiment 2

Based on the method for recording a system log provided by embodiment 1,the example embodiment provides a device for recording a system log.FIG. 2 is a structural diagram of the device for recording a system logaccording to an example embodiment of the disclosure, and as shown inFIG. 2, the device includes: an acquisition element 202, configured toacquire the sequence information of a virtual log file in a system,wherein the virtual log file is a preset file for the system, and thevirtual log file includes the sequence information, and the sequenceinformation is used for determining a position of a log file which iswritten last time by the system; and a first determination element 204,connected to the acquisition unit 20 and configured to determine the logfile which is written last time by the system according to the sequenceinformation of the virtual log file, and write the determined log file.

In the example embodiment, when system log recording is performed, thesequence information of the virtual log file is acquired at first, thelast log recording position in the system is determined according to thesequence information of the virtual log file, and the system iscontrolled to sequentially continue performing log recording after thelast log recording position in the system is determined; and by such alog recording manner, the problem in the related art that log filerecording sequence disorder caused by the influence of changing ofsystem time on a log recording process of the system with a requirementon a recording time length of the log files in the prior art iseffectively solved, and the sequence of system log recording is notinfluenced by changes in the system time.

In an example embodiment of the disclosure, the device is alsooptimized, and specifically, as shown in FIG. 3, the device furtherincludes: a second determination element 302, configured to determine amaximum value N of the number of log files required to be reserved inthe system according to the requirement of the system on a length oftime when a log is written, before acquiring the sequence information ofthe virtual log file in the system; a creation element 304, connected tothe second determination element 302 and configured to create N+1sequence numbers, recorded as 1, 2 . . . N, N+1 respectively, thesequence numbers corresponding to the log files which are sequentiallycreated, wherein a sequence number of a log file which is created forthe first time is 1, a sequence number of a log file which is createdfor the (N+1)th time is N+1, a sequence number of a log file which iscreated for the (N+2)th time is 1, and so on; and a generation element306, connected to the creation element 304 and configured to generatethe sequence information of the virtual log file according to thecreated sequence numbers and a log recording status of a current logfile.

For example, if the system requires the reservation of log records ofthree days with one log file for one day, it can be determined that themaximum number N of the log files required to be reserved in the systemis 3, and after N is determined, the sequence numbers are created.Specifically, the number of the created sequence numbers is N+1=4, i.e.1, 2, 3 and 4 respectively. The four sequence numbers correspond to thelog files which are sequentially created, that is, a sequence numbercorresponding to a log file which is created by the system on the firstday is 1, a sequence number corresponding to a log file which is createdby the system on the second day is 2, a sequence number corresponding toa log file which is created by the system on the third day is 3; andbecause the system only requires the reservation of the log records ofthree days, a sequence number corresponding to a log file which iscreated by the system on the fourth day is 4, then the log file which iscreated on the first day and corresponds to the sequence number 1 iscleared, a sequence number corresponding to a log file which is createdby the system on the fifth day is 1, the log file which is created onthe second day and corresponds to the sequence number 2 is cleared, andso on. After the creation of the sequence numbers is finished, logrecording statuses (whether or not there are recorded logs) in the logfiles corresponding to the sequence number 1, the sequence number 2, thesequence number 3 and the sequence number 4 are sequentially judged, andthe sequence information of the virtual log file is generated accordingto a judgment result.

In addition, the disclosure also provides an example scheme forgenerating the sequence information of the virtual log file, andspecifically, as shown in FIG. 4, the generation element 306 includes: ajudgment component 402, configured to sequentially judge whether or notthere is a log recorded under each of the created sequence numbers; anda generation component 404, connected to the judgment component 402 andconfigured to acquire a minimum sequence number n from the sequencenumbers under which there are no logs recorded, and generate thesequence information of the virtual log file according to the minimumsequence number n, wherein the sequence information is used forindicating that the log file which is written last time by the system isa log file with a sequence number n−1.

Embodiment 3

Based on the method for recording a system log provided by embodiment 1and the device for recording a system log provided by embodiment 2, theexample embodiment provides another method for recording a system log.In an example embodiment, a virtual log file occupies a sequence number,the sequence number occupied by the virtual log file can be used as thesequence information of the virtual log file, and the sequence numberdynamically changes along with changes in statuses of logs in log filesin a system, and a position of a last log file where the log is recordedin the system is determined according to the sequence number. Specificdescription is given below.

If the system requires the reservation of log records of the last X dayswith one log file for one day, a set sequence number value range is [1,(X+1)], wherein (X+1) sequence numbers correspond to X log files and avirtual log file respectively. Here, it is important to note that thevirtual log file does not actually exist, and can be a logicallyexisting file only occupying the sequence number, and during specificimplementation, the virtual log file can also be a null file or exist inform of another file with a fixed characteristic as long as the virtuallog file can be distinguished from one or more normal files.

The searching algorithm of the sequence number of the virtual log fileis as follows: a directory of the log records is traversed, whether ornot the log file corresponding to the sequence number exists issequentially judged from a sequence number 2 to a sequence number (X+1),and if a log file corresponding to a sequence number does not exist, itis determined that the sequence number is occupied by the virtual logfile (that is, the sequence number of the virtual log file is thesequence number). If the log files corresponding to the sequence number2 to the sequence number (X+1) all exist, the sequence number of thevirtual log file is 1. In any running time bucket of the system, such analgorithm can be used for calculating the sequence number of the virtuallog file at that moment.

Specifically, the method includes the following steps:

Step 1: the system is started, the searching algorithm of the sequencenumber of the virtual log file is used for determining a sequence numberVIXdex of the virtual log file at first. In an example embodiment, ifthere is no log file under a log directory, the obtained sequence numberVIXdex of the virtual log file is 2. Then, a sequence number CIXdex of acurrent log file is calculated according to formulaCIXdex=(VIXdex−1)%(X+1), where % is remainder calculation operation.Finally, the log is recorded in the log file with the sequence numberCIXdex. If not existing, the log file with the sequence number CIXdex isgenerated. In an example embodiment, if there is no log file in the logdirectory, the obtained sequence number CIXdex of the current recordinglog file is 1, that is, if there is no log file when the system isstarted, the logs are recorded from the log file with a sequence number1.

Step 2: after one day, system time and date are discovered to be changedwhen logs are recorded, the recording of the current log file is ended,and the current log file is switched. In an example embodiment, asequence number CIXdex' of a log file to which the current log file isrequired to be switched is calculated according to formulaCIXdex'=(CIXdex+1)%(X+1). Then, whether or not the log file with thesequence number CIXdex' exists is judged, and if existing, the log filewith the sequence number CIXdex' is deleted at first, and another logfile with the sequence number CIXdex' is generated for log recording.

Step 3: Step 2 is repeated until the system is restarted.

Step 4: Step 1 to Step 3 are repeated.

As shown in FIG. 5, the process of the method is specifically describedby taking X=3 as an example. On the first day, a log is recorded by thesystem in a log file with a sequence number 1, and the sequence numberof the virtual log file is 2; on the second day, a log is recorded bythe system in a log file with a sequence number 2, and the sequencenumber of the virtual log file is 3; and so on.

From the above, the disclosure has the advantages as follows: 1, therequirement of the system on the recording time length of the log filescan be met; and 2, when the system time is changed, the recordingsequence of the log files can still be ensured.

INDUSTRIAL APPLICABILITY

From the above, the method and device for recording a system logprovided by the embodiments of the disclosure have the beneficialeffects as follows: when system log recording is performed, the sequenceinformation of the virtual log file is acquired at first, the last logrecording position in the system is determined according to the sequenceinformation of the virtual log file, and the system is controlled tosequentially continue performing log recording after the last logrecording position in the system is determined, so that the sequence ofsystem log recording is not influenced by changes in the system time.

Although the example embodiments of the disclosure have been disclosedfor the aims in the embodiments, those skilled in the art should realizethat the disclosure can have various improvements, additions andreplacements. Therefore, the embodiments are not intended to limit thescope of the disclosure.

What is claimed is:
 1. A control method for recording a system log,comprising: acquiring sequence information of a virtual log file in asystem, wherein the virtual log file is a preset file for the system,and the virtual log file comprises the sequence information, and thesequence information is used for determining a position of a log filewhich is written last time by the system; and determining the log filewhich is written last time by the system according to the sequenceinformation of the virtual log file, and writing the determined logfile.
 2. The method according to claim 1, wherein before acquiring thesequence information of the virtual log file in the system, the methodfurther comprises: determining a maximum value N of the number of logfiles required to be reserved in the system according to a requirementof the system on a length of time when a log is written; creating N+1sequence numbers, recorded as 1, 2 . . . N, N+1 respectively, thesequence numbers corresponding to log files which are sequentiallycreated, wherein a sequence number of a log file which is created forthe first time is 1, a sequence number of a log file which is createdfor the (N+1)th time is N+1, a sequence number of a log file which iscreated for the (N+2)th time is 1, and so on; and generating thesequence information of the virtual log file according to the createdsequence numbers and a log recording status of a current log file. 3.The method according to claim 2, wherein generating the sequenceinformation of the virtual log file according to the created sequencenumbers and the log recording status of the current log file comprises:sequentially judging whether or not there is a log recorded under eachof the created sequence numbers; and acquiring a minimum sequence numbern from sequence numbers under which there are no logs recorded, andgenerating the sequence information of the virtual log file according tothe minimum sequence number n, wherein the sequence information is usedfor indicating that the log file which is written last time by thesystem is a log file with a sequence number n−1.
 4. A control device forrecording a system log, comprising: an acquisition element configured toacquire sequence information of a virtual log file in a system, whereinthe virtual log file is a preset file for the system, and the virtuallog file comprises the sequence information, and the sequenceinformation is used for determining a position of a log file which iswritten last time by the system; and a first determination elementconfigured to determine the log file which is written last time by thesystem according to the sequence information of the virtual log file,and write the determined log file.
 5. The device according to claim 4,comprising: a second determination element configured to determine amaximum value N of the number of log files required to be reserved inthe system according to a requirement of the system on a length of timewhen a log is written, before acquiring the sequence information of thevirtual log file in the system; a creation element configured to createN+1 sequence numbers, recorded as 1, 2 . . . N, N+1 respectively, thesequence numbers corresponding to log files which are sequentiallycreated; and a generation element configured to generate the sequenceinformation of the virtual log file according to the created sequencenumbers and a log recording status of a current log file.
 6. The deviceaccording to claim 5, wherein the generation element comprises: ajudgment component configured to sequentially judge whether or not thereis a log recorded under each of the created sequence numbers; and ageneration component configured to acquire a minimum sequence number nfrom sequence numbers under which there are no logs recorded, andgenerate the sequence information of the virtual log file according tothe minimum sequence number n, wherein the sequence information is usedfor indicating that the log file which is written last time by thesystem is a log file with a sequence number n−1.